package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysLogMapper;
import com.itheima.pojo.SysLog;
import com.itheima.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

/**
 * @ClassName SysLogServiceImpl
 * @Description 操作日志 服务实现类
 * @Author pengkaka
 * @Date 2020/8/5 14:44
 * @Version 1.0
 **/
@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    // 分页获取操作日志列表
    @Override
    public Page<SysLog> getPageList(Map<String, Object> searchMap) {
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Example example = new Example(SysLog.class);
        // 排序功能实现:字段名引号问题
        example.setOrderByClause("create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (searchMap.get("userName") != null && !"".equals(searchMap.get("userName"))) {
            criteria.andLike("userName", "%" + searchMap.get("userName") + "%");
        }
        if (searchMap.get("operation") != null && !"".equals(searchMap.get("operation"))) {
            criteria.andLike("operation", "%" + searchMap.get("operation") + "%");
        }
        if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime"))) {
            String startTime = (String) searchMap.get("startTime");
            String[] time = startTime.split(",");
            if (time.length == 2) {
                criteria.andBetween("createTime", time[0], time[1]);
            }
        }
        criteria.andEqualTo("status", "1");
        Page<SysLog> pageList = (Page<SysLog>) sysLogMapper.selectByExample(example);
        return pageList;
    }
}
